package leetcode.stack;

import java.util.LinkedList;
import java.util.List;

public class SlidingWindow {
    public static int[] process(int[] nums,int k){
        if (nums==null || k<1 || nums.length<k){
            return new int[]{0};
        }

        int[] res = new int[nums.length-k+1];
        int n = 0;
        LinkedList<Integer> qmax = new LinkedList<>();

        for (int i = 0; i < nums.length ; i++) {
            while (!qmax.isEmpty() && nums[i]>nums[qmax.peekLast()]){
                qmax.pollLast();
            }
            qmax.addLast(i);
            if (qmax.peekFirst()==i-k){
                qmax.pollFirst();
            }
            if (i>=k-1){
                res[n++]=nums[qmax.peekFirst()];
            }
        }
        return res;
    }
}
